package net.animats.cli;

import java.util.regex.*;
import java.util.List;
import java.util.ArrayList;

public abstract class Verb {
	
	private Pattern syntax;
	private String command;
	private List<String> argumentType = new ArrayList<String>();

	public static void main (String[] args) {
		
	}

	public Verb (String _syntax) {
		String regex = "^";

		Pattern tokens = Pattern.compile ("(\\{integer\\}|\\{string\\}|\\{.*?\\})");
		Matcher tokenMatcher = tokens.matcher(_syntax);
		while (tokenMatcher.find()) {
			// TOKEN REFERENCE WAS FOUND, STORE ITS TYPES
			argumentType.add(tokenMatcher.group(1));
		}

		regex += tokenMatcher.replaceAll("([^\\\\s]+?)") + "$";

		//System.out.println("original gramamr = " + _syntax);
		//System.out.println("generated regex = " + regex);

		command = _syntax;
		syntax = Pattern.compile(regex);
	}

	public String helpText() {
		return command;
	}

	public String getCommand() {
		return command;
	}

	public Pattern getSyntax() {
		return syntax;
	}

	public Matcher getMatcher(String _command) {
		return syntax.matcher(_command);
	}

	public String getArgumentType(int _index) {
		if (_index >= argumentType.size()) {
			System.out.println("Asking for argument " + _index + " of " + syntax + " but only has " + argumentType.size() + " arguments.");
			return "fail";
		} else {
			return argumentType.get(_index);
		}
	}

	public abstract int doVerb(Object[] _objects, Parser _parser);
}

